package com.hllxd.hism.star.basic.service.dao.http;

import com.alibaba.fastjson.JSONObject;
import com.hllxd.hism.star.basic.service.config.properties.YarnProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * Yarn的应用操作
 * @author lixiyong
 * @date 2019-01-02 10:11
 **/
@Slf4j
@Component("rockAssemApiMapper")
public class RockAssemApiMapperImpl implements RockAssemApiMapper {

    @Autowired
    private YarnProperties properties;

    private static String URL = "https://dohko-api.tiaofangzi.com";
    private static String ACCESS_TOKEN = "p0r1o2d3u4c5t6O7u8t9Store0123456";

    @PostConstruct
    private void initialize() {
        URL = properties.storeUrl;
        ACCESS_TOKEN = properties.accessToken;
    }

    public static boolean isOK(JSONObject obj) {
        String status = obj.getString("code");
        return (status!=null && "000".equals(status));
    }

    public static class KillRequest{
        /**off 离线*/
        public String cloud;
        /**任务id（如：root.queue_cloud_rtq）*/
        public String appid;
        /** 提交用户名*/
        public String appuser;
        /**秘钥（需要磐石平台申请）*/
        public String token;

        public static KillRequest build(String cloud, String appid, String appUser) {
            KillRequest thiz = new KillRequest();
            thiz.cloud = cloud;
            thiz.appid = appid;
            thiz.appuser = appUser;
            thiz.token = ACCESS_TOKEN;
            return thiz;
        }
    }

    public static class KillRespond {
        public String ret;
        public String code;
        public String message;
        public String data;
        public Boolean success;
    }
    @Override public Boolean yarnKillApp(String cloud, String appid, String appUser) {
//        if(StringUtil.isEmpty(appid)) {
//            log.warn("params is invalid.");
//            throw new BusinessException("302","params is invalid.");
//        }
//        KillRequest request = KillRequest.build(cloud, appid, appUser);
//
////        JSONObject response = OkHttpUtil.ajaxPost(JSON.toJSONString(request),"http://api.assem.bj58.zp.net/yarn/yarnKillApp2");
//        if(response==null) {
//            throw new BusinessException("502","http request remote failed.");
//        }
//        if(!isOK(response)) {
//            log.warn("result is failed. {}", request);
//            throw new BusinessException("504", "remote service response is error.");
//        }
        return true;
    }
}
